package com.example.administrator.hqq.activity.group;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.home.SettingGroupActivity;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.bean.GroupEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.GetLocationUtils;
import com.example.administrator.hqq.utils.HintWordsUtils;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;
import okhttp3.Call;

public class SkipCreateGroupActivity extends MyActivity {

    @BindView(R.id.return_auto)
    AutoRelativeLayout returnAuto;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.continue_setting)
    Button continueSetting;
    @BindView(R.id.create_group)
    Button createGroup;
    private String TAG=this.getClass().getSimpleName();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skip_create_group);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @OnClick({R.id.continue_setting, R.id.create_group})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.continue_setting:
                addGroup(true);
                break;
            case R.id.create_group:
                addGroup(false);
                break;
        }
    }

    private void addGroup(final boolean b) {
        GetBuilder url = OkHttpUtils.get().url(UrlFactory.addGroup()).tag(this);
        url.addParams(LoongsConfig.user_id, SPUtils.getInstance().getString(LoongsConfig.user_id))
                .addParams(LoongsConfig.group_id,getIntent().getStringExtra(LoongsConfig.group_id))
                .addParams(LoongsConfig.topic_id, getIntent().getStringExtra(LoongsConfig.topic_id))
                .addParams(LoongsConfig.title, getIntent().getStringExtra(LoongsConfig.title))
                .addParams(LoongsConfig.url, getIntent().getStringExtra(LoongsConfig.url))
                .addParams(LoongsConfig.max_member, getIntent().getStringExtra(LoongsConfig.max_member))
                .addParams(LoongsConfig.location_prefer, getIntent().getStringExtra(LoongsConfig.location_prefer))
                .addParams(LoongsConfig.gender_prefer, getIntent().getStringExtra(LoongsConfig.gender_prefer))
                .addParams(LoongsConfig.age_prefer, getIntent().getStringExtra(LoongsConfig.age_prefer))
                .addParams(LoongsConfig.question_amount, "0")
                .addParams(LoongsConfig.question, "")
                .addParams(LoongsConfig.introduction,"")
                .addParams(LoongsConfig.location, GetLocationUtils.getLocation())
                .addParams(LoongsConfig.city_code, GetLocationUtils.getCityCode())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String s,int id) {
                Log.e(TAG, "onResponse: " + s);
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        HintWordsUtils.showMarkeWords(jsonObject.getInt(LoongsConfig.code),jsonObject.getString(LoongsConfig.description));
                        if (jsonObject.getInt(LoongsConfig.code)==200)
                            {
                                GroupEvent groupEvent = new GroupEvent();
                                groupEvent.setGroupId("");
                                groupEvent.setType(GroupEvent.Type.CREATE_GROUP);
                                EventBus.getDefault().post(groupEvent);
                                String string = getIntent().getStringExtra(LoongsConfig.group_id);
                                if (b) {
                                    Intent intent = new Intent(SkipCreateGroupActivity.this, SettingGroupActivity.class);
                                    intent.putExtra(LoongsConfig.group_id,string);
                                    startActivity(intent);
                                }
                            else{
                                    SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.GROUP);
                                    SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put( LoongsConfig.Conversation.ConversationID,string);
                                    RongIM.getInstance().startConversation(SkipCreateGroupActivity.this, Conversation.ConversationType.GROUP,string,"话圈圈");
                                }
                                ActivityUtils.finishActivity(SelectNumbeActivity.class);
                                ActivityUtils.finishActivity(CreateGroupChatActivity.class);
                                finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }
        });
    }
    @Subscribe (threadMode = ThreadMode.MAIN)
    public  void Event(String s){

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }
}
